﻿
namespace Orchard.OData {
    using Orchard.Mvc.Routes;
    using Orchard.OData.Services;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Routing;

    public interface IODataRouteHandler : IRouteHandler, IDependency {
    }
        
    public sealed class ODataRouteHandler : IODataRouteHandler {
        private readonly IODataHttpHandler oDataHttpHandler;
        public ODataRouteHandler(IODataHttpHandler oDataHttpHandler) {
            this.oDataHttpHandler = oDataHttpHandler;
        }

        IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) {
            return this.oDataHttpHandler;
        }
    }

    public sealed class RouteProvider : IRouteProvider {      
        private readonly IODataRouteHandler odataRouteHandler;
        public RouteProvider(IODataRouteHandler odataRouteHandler) {
            this.odataRouteHandler = odataRouteHandler;
        }

        IEnumerable<RouteDescriptor> IRouteProvider.GetRoutes() {
            return new List<RouteDescriptor>() { 
                new RouteDescriptor { 
                    Priority = 9999, 
                    Route = new Route("odata/{*pathInfo}", this.odataRouteHandler) 
                },
            };
        }

        void IRouteProvider.GetRoutes(ICollection<RouteDescriptor> routes) {
            (this as IRouteProvider)
                .GetRoutes()
                .ToList()
                .ForEach(routes.Add);
        }
    }
}